package com.yhsimon.springmvc;

import java.util.ArrayList;
import java.util.List;

public class DispatcherServlet {
    public static List<HandlerAdapter> handlerAdapters=new ArrayList<>();

    public DispatcherServlet(){
        handlerAdapters.add(new HttpHandlerAdapter());
        handlerAdapters.add(new SimpleHandlerAdapter());
        handlerAdapters.add(new AnnotationHandlerAdapter());
    }

    public void doDispatcher(){
        //模拟SpringMVC从request取handler的对象
        //适配器可以获取到希望的Controller
        AnnotationController controller = new AnnotationController();
        //得到对应的适配器
        HandlerAdapter adapter=getHandler(controller);
        //通过适配器执行队形的controller对应的方法
        adapter.handle(controller);
    }

    public HandlerAdapter getHandler(Controller controller){
        for (HandlerAdapter adapter:this.handlerAdapters){
            if(adapter.support(controller)){
                return adapter;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        new DispatcherServlet().doDispatcher();
    }
}
